%�������깤ʱ��
function [f1,setime,manufacturetime] = TimeCost(chrome,m_no,availableMachine,Vcut,LENGTHPlate,Setup,Setdown)

    seqence = chrome(1,1:m_no);
    machineDis = chrome(1,m_no+1:end);
    machineKind = size(availableMachine,2);
    usedMachine = zeros(1,machineKind);
    indexToSameKind=zeros(1,machineKind);%��ǰ�����ͬ�������и���е�ĳһ̨
    
    dim=max(availableMachine);
    setup_machineStartTime=zeros(machineKind,dim);%��ͬ�����͵��и��������ֹһ��ʱ����Ҫ����ͬ�������е�ÿһ��������ʼ�и��ʱ��
    setdown_machineStartTime=zeros(machineKind,dim);
    for i=1:machineKind
       setup_machineStartTime(i,availableMachine(i)+1:end)=inf; 
    end
    
    setup_machineEndTime=setup_machineStartTime;%��ͬ�����͵��и��������ֹһ��ʱ����Ҫ����ͬ�������е�ÿһ�������ϵ�ǰ�ְ�Ľ����и��ʱ��
    for i=1:machineKind
        setdown_machineStartTime(i,availableMachine(i)+1:end)=inf;
    end
    setdown_machineEndTime=setdown_machineStartTime;
    manufacturetime = zeros(2,m_no);%ÿ���ĵĿ�ʼ�ӹ�ʱ��ͽ����ӹ�ʱ��㡣   
    for i=1:m_no
        num = 1;%�ڼ������͵��и���������֣�
        while(1)
            if (num==machineDis(i))
               break;              %(����������ж�Ӧ����Ļ���)
            end
            num = num+1; 
        end
        usedMachine(1,num) = usedMachine(1,num) + 1;
        
        v=Vcut(num);
        length = LENGTHPlate(seqence(i));
         setup_time=Setup(seqence(i));
         setdown_time=Setdown(seqence(i));         
        if usedMachine(1,num)>availableMachine(1,num)
            %�ȴ�
            [temp,indexToSameKind(1,num)]=min(setdown_machineEndTime(num,:));%����ͬ�����͵Ļ�������С�����ӹ�ʱ�䣨ʱ�̣���ѡ��������е�һ̨����
            setup_startime = temp;
            setup_end_time=setup_startime+setup_time;
            startime=setup_end_time;           
            endtime = startime + round(length/v);
            setdo_startime=endtime;
            setdown_endtime=setdo_startime+setdown_time;
            setup_machineStartTime(num,indexToSameKind(1,num))=setup_startime;
            setup_machineEndTime(num,indexToSameKind(1,num))=setup_end_time;
            machineStartTime(num,indexToSameKind(1,num))=startime;%���¸û��������·���İ�ĵ��и�ʱ��
            machineEndTime(num,indexToSameKind(1,num))=endtime;
            setdown_machineStartTime(num,indexToSameKind(1,num))=setdo_startime;
            setdown_machineEndTime(num,indexToSameKind(1,num))=setdown_endtime;
            manufacturetime(1,i) = startime;%���°�ĵ��и�ӹ�ʱ��
            manufacturetime(2,i) = endtime;
            manufacturetime(3,i) = indexToSameKind(1,num);%����øְ��ɸ������еĵڼ�̨�����ӹ�
            setime(1,i)=setup_startime;
            setime(2,i)=setup_end_time;
            setime(3,i)=setdo_startime;
            setime(4,i)=setdown_endtime;
            
        else
            %ֱ���и�ӹ�
            [temp,indexToSameKind(1,num)]=min(setdown_machineEndTime(num,:));
            setup_startime = temp;%(׼���Ŀ�ʼʱ��)
            setup_end_time=setup_startime+setup_time;%��׼���Ľ���ʱ�䣩
            startime=setup_end_time;           %����ļӹ���ʼʱ�䣩
            endtime = startime + round(length/v);%����ļӹ�����ʱ�䣩
            setdo_startime=endtime;%(��ʰ�Ŀ�ʼʱ��)
            setdown_endtime=setdo_startime+setdown_time;%����ʰʱ��Ľ���ʱ�䣩
            setup_machineStartTime(num,indexToSameKind(1,num))=setup_startime;%������׼���Ŀ�ʼʱ�䣩
            setup_machineEndTime(num,indexToSameKind(1,num))=setup_end_time; %������׼���Ľ���ʱ�䣩
            machineStartTime(num,indexToSameKind(1,num))=startime; %���¸û��������·���İ�ĵ��и�ʱ��
            machineEndTime(num,indexToSameKind(1,num))=endtime;  %���������и����ʱ�䣩
            setdown_machineStartTime(num,indexToSameKind(1,num))=setdo_startime;%��������ʰ�Ŀ�ʼʱ�䣩
            setdown_machineEndTime(num,indexToSameKind(1,num))=setdown_endtime;%��������ʰ�Ľ���ʱ�䣩
            manufacturetime(1,i) = startime;%���°�ĵ��и�ӹ�ʱ��
            manufacturetime(2,i) = endtime;%����ĵ��и�ӹ�����ʱ�䣩
            manufacturetime(3,i) = indexToSameKind(1,num);%����øְ��ɸ������еĵڼ�̨�����ӹ�
            setime(1,i)=setup_startime;   %��׼���Ŀ�ʼʱ�䣩
            setime(2,i)=setup_end_time;   %��׼���Ľ���ʱ�䣩
            setime(3,i)=setdo_startime;   %����ʰ�Ŀ�ʼʱ�䣩
            setime(4,i)=setdown_endtime;  %����ʰ�Ľ���ʱ�䣩
        end
        
    end
    
    f1 = max(setime(4,:));
end